const mysqlDB = require('../config/mysqlDB.config')

const Aritle = mysqlDB.import('./aritle')
const Tag = mysqlDB.import('./tag')

module.exports = (sequelize, DataTypes) => {
  const AritleToTag = sequelize.define(
    'aritleToTag',
    {
      id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true
      },
      tag_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0
      },
      aritle_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0
      }
    },
    {
      timestamps: false,
      freezeTableName: true
    }
  )
  Aritle.belongsToMany(Tag, {
    // 会在  Aritle 中生成 addTags setTags getTags
    as: 'tags',
    through: AritleToTag,
    foreignKey: 'aritle_id',
    constraints: false
  })

  Tag.belongsToMany(Aritle, {
    as: 'aritles',
    through: AritleToTag,
    foreignKey: 'tag_id',
    constraints: false
  })
  return AritleToTag
}
